package org.lyg.controller;

import com.github.pagehelper.PageInfo;

import org.apache.shiro.SecurityUtils;
import org.lyg.domain.Blog;
import org.lyg.domain.BlogCondition;
import org.lyg.domain.User;
import org.lyg.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class MainController {
    private static final int BLOG_PAGE_SIZE = 10;
    @Autowired
    IBlogService blogService;

    @RequestMapping("/")
    public String root() {
        return "redirect:home";
    }

    @RequestMapping("/home")
    public String home(ModelMap modelMap) {
        return home(1,modelMap);
    }

    @RequestMapping("/home/list/{page}")
    public String home(@PathVariable("page") int page, ModelMap modelMap) {
        User userSelf = (User) SecurityUtils.getSubject().getPrincipal();
        modelMap.addAttribute("userSelf", userSelf);

        PageInfo<Blog> blogPageInfo = blogService.list(BLOG_PAGE_SIZE, page, new BlogCondition(null, null, null, false));
        modelMap.addAttribute("blogPageInfo", blogPageInfo);
        return "home";
    }

    @RequestMapping("/unanth")
    public String unanth(){
        return "unanth";
    }

    @RequestMapping("/api")
    @ResponseBody
    public String api(HttpServletResponse response) {
        return "Hello";
    }
}
